package com.atguigu.order.controller;


import com.aiguigu.commonutils.R;
import com.aiguigu.commonutils.ResultCode;
import com.atguigu.order.service.PayLogService;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author Y丶HS练习谷粒商城
 * @since 2022-10-09
 */
@RestController
@RequestMapping("/order/payLog")
public class PayLogController {
    @Resource
    private PayLogService payLogService;

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @ApiOperation("生成订单二维码")
    @GetMapping("/saveEntity/{orderNo}")
    public R saveEntity(@PathVariable String orderNo){
        Map map =  payLogService.createNative(orderNo);
        log.info("saveEntity#map:{}",map);
        return R.ok().data(map);
    }
    @ApiOperation("查询订单支付状态")
    @GetMapping("/queryPayStatus/{orderNo}")
    public R queryPayStatus(@PathVariable String orderNo){
       Map<String, String> map =  payLogService.queryPayStatus(orderNo);
        log.info("queryPayStatus#map:{}",map);
       if(Objects.isNull(map)){
//           throw new GuLiException(ResultCode.ERROR,"支付出错了");
          return R.error().message("支付出错了");
       }
        // 获取订单状态
        if("SUCCESS".equals(map.get("trade_state"))){
            // 支付表中添加记录,更新订单状态
            payLogService.updateOrderStatus(map);
            return R.ok().data(map);
        }
        return R.error().code(ResultCode.IN_PAY).message("支付中");
    }
}

